import { Ball } from "./ball";
import { Context } from "./context";
import { plist } from "../data/pdata";
import { Tools } from "./tools";
export class Main {

    constructor(canvas) {
        this.context = new Context();
        console.log('=========', canvas);
        this.context.init({canvas: canvas});
        this.context.start();
    }

    test() {
        let ball = new Ball(this.context, 100, 100, 25);
        ball.setPList(Tools.smoothPoints(plist), 0);
        ball.setHeader(true);
        this.p = ball;
        this.dfsCreate(20);
    }

    onMouseDown(x, y) {
        let clickedBalls = Tools.hitTestAll({x: x, y: y, r: 0}, this.context.collections.ball);
        if (clickedBalls.length > 0) {
            // delete
            for (let i = 0; i < clickedBalls.length; i++) {
                clickedBalls[i].die();
            }
        }
    }

    dfsCreate(n) {
        if (n <= 0) {
            // this.p.setTail(this.p);
            return;
        }
        let ball = new Ball(this.context, 100, 100, 25);
        ball.setPList(Tools.smoothPoints(plist), 0);
        this.p.setNext(ball);
        ball.setPrev(this.p);
        this.p = ball;
        Tools.setTimeoutRAF(() => this.dfsCreate(n - 1), 300);
    }

}